﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:MVVMFramework.View">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/MVVMFramework.View;component/Themes/Colors.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>

    <ControlTemplate x:Key="ExpanderButtonControlTemplate" TargetType="{x:Type ToggleButton}">
        <Border x:Name="border" BorderThickness="1" CornerRadius="2" Background="{StaticResource TransparentBrush}" 
                BorderBrush="{StaticResource TransparentBrush}" Height="18" HorizontalAlignment="Left" VerticalAlignment="Stretch" Width="18">
            <Grid>
                <Image x:Name="iconCollapse" Visibility="Collapsed" HorizontalAlignment="Center" Height="Auto" Stretch="None" VerticalAlignment="Center"
                           Source="/MVVMFramework.View;Component/Images/Collapse.png" Width="Auto" Margin="2"/>

                <Image x:Name="iconExpand" Visibility="Visible" HorizontalAlignment="Center" Height="Auto" Stretch="None" 
                           VerticalAlignment="Center" Source="/MVVMFramework.View;Component/Images/Expand.png" Width="Auto" Margin="2"/>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsChecked" Value="True">
                <Setter Property="Visibility" TargetName="iconCollapse" Value="Visible"/>
                <Setter Property="Visibility" TargetName="iconExpand" Value="Collapsed"/>
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Background" TargetName="border" Value="{StaticResource PressedInnerBackgroundBrush}"/>
                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource CommonBorderBrush}"/>
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsMouseOver" Value="True"/>
                    <Condition Property="IsPressed" Value="False"/>
                </MultiTrigger.Conditions>
                <Setter Property="Background" TargetName="border" Value="{StaticResource HoverInnerBackgroundBrush}"/>
                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource CommonBorderBrush}"/>
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsKeyboardFocusWithin" Value="True"/>
                    <Condition Property="IsPressed" Value="False"/>
                </MultiTrigger.Conditions>
                <Setter Property="Background" TargetName="border" Value="{StaticResource HoverInnerBackgroundBrush}"/>
                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource CommonBorderBrush}"/>
            </MultiTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <ControlTemplate x:Key="VerticalExpanderControlTemplate" TargetType="local:Expander">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" MinHeight="18"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" Foreground="Gray" Margin="0,0,5,0" Text="{TemplateBinding Header}"/>
            <Separator Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" Margin="0"/>

            <ToggleButton Name="PART_ToggleButton" Grid.Row="0" Grid.Column="2"
                          IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsExpanded, Mode=TwoWay}"
                          HorizontalAlignment="Right"
                          Visibility="{TemplateBinding ShowExpandButton, Converter={StaticResource BoolToVisibilityConverter}}"
                          VerticalAlignment="Top" Focusable="False" Template="{StaticResource ExpanderButtonControlTemplate}" />

            <Border Name="PART_ContentHolder" Grid.Row="1" Grid.ColumnSpan="3"
                    Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ContentHeight, Mode=TwoWay}" 
                    Visibility="{TemplateBinding IsExpanded, Converter={StaticResource BoolToVisibilityConverter}}"/>
        </Grid>

        <ControlTemplate.Triggers>
            <Trigger Property="ExpandButtonDirection" Value="Up">
                <Setter Property="RenderTransform" TargetName="PART_ToggleButton">
                    <Setter.Value>
                        <RotateTransform Angle="180" CenterX="9" CenterY="9" />
                    </Setter.Value>
                </Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!-- ****************** Expand Panel ******************** -->
    <Style TargetType="local:Expander">
        <Setter Property="Template" Value="{StaticResource VerticalExpanderControlTemplate}"/>
    </Style>

</ResourceDictionary>